﻿local path = "Interface\\AddOns\\eMap\\Textures\\"

local function colorCode(fileName)
	local colorRGB = RAID_CLASS_COLORS[fileName]
	if colorRGB then
		return format("|CFF%2x%2x%2x", colorRGB.r*255, colorRGB.g*255, colorRGB.b*255)
	else
		return NORMAL_FONT_COLOR_CODE
	end
end

--tooptip
local function MapUnit_OnEnter(self, motion, map)
	-- Adjust the tooltip based on which side the unit button is on
	local x, y = self:GetCenter()
	local parentX, parentY = self:GetParent():GetCenter()
	if ( x > parentX ) then
		if map == "WorldMap" then
			WorldMapTooltip:SetOwner(self, "ANCHOR_LEFT")
		else
			GameTooltip:SetOwner(self, "ANCHOR_LEFT")
		end
	else
		if map == "WorldMap" then
			WorldMapTooltip:SetOwner(self, "ANCHOR_RIGHT")
		else
			GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
		end
	end

	local unitButton, unit
	local newLineString = ""
	local tooltipText = ""
	local name, nameTemp, server, subgroup, class, fileName


	for i=1, MAX_PARTY_MEMBERS do
		unitButton = _G[map.."Party"..i]
		if ( unitButton:IsVisible() and MouseIsOver(unitButton) ) then
			name = UnitName(unitButton.unit)
			if ( PlayerIsPVPInactive(unitButton.unit) ) then
				tooltipText = tooltipText..newLineString..format(PLAYER_IS_PVP_AFK, name)
			else
				class, fileName = UnitClass(unitButton.unit)
				tooltipText = tooltipText..newLineString..class.." "..colorCode(fileName)..name.."|r"
			end
			newLineString = "\n"
		end
	end

	for i=1, MAX_RAID_MEMBERS do
		unitButton = _G[map.."Raid"..i]
		unit = unitButton.unit
		if ( unitButton:IsVisible() and MouseIsOver(unitButton) ) then
			if ( unitButton.name ) then
				-- Handle players not in your raid or party, but on your team
				if ( PlayerIsPVPInactive(unitButton.name) ) then
					tooltipText = tooltipText..newLineString..format(PLAYER_IS_PVP_AFK, unitButton.name)
				else
					tooltipText = tooltipText..newLineString..unitButton.name
				end
			else
				if ( PlayerIsPVPInactive(unitButton.unit) ) then
					tooltipText = tooltipText..newLineString..format(PLAYER_IS_PVP_AFK, UnitName(unitButton.unit))
				else
					for j=1, MAX_RAID_MEMBERS do
						nameTemp, _, subgroup, _, class, fileName = GetRaidRosterInfo(j)
						_, _, name, server = string.find(nameTemp or "", "([^%-]+)%-(.+)")
						if UnitName(unit) == name or UnitName(unit) == nameTemp then							
							if server then
								nameTemp = colorCode(fileName)..name.."|r".."|CFFFFFFFF-|r|CFFFFD200"..server.."|r"
							else
								nameTemp = colorCode(fileName)..nameTemp.."|r"
							end
							tooltipText = tooltipText..newLineString..subgroup.."队 "..class.." "..nameTemp
							break
						end
					end
				end
			end
			newLineString = "\n"
		end
	end

	local numVehicles = GetNumBattlefieldVehicles()
	for _, v in pairs(MAP_VEHICLES) do
		if ( v:IsVisible() and MouseIsOver(v) ) then
			if ( v.name ) then
				tooltipText = tooltipText..newLineString..v.name
			end
			newLineString = "\n"
		end
	end

	for i = 1, NUM_WORLDMAP_DEBUG_OBJECTS do
		unitButton = _G["WorldMapDebugObject"..i]
		if ( unitButton:IsVisible() and MouseIsOver(unitButton) ) then
			tooltipText = tooltipText..newLineString..unitButton.name
			newLineString = "\n"
		end
	end
	if map == "WorldMap" then
		WorldMapTooltip:SetText(tooltipText)
		WorldMapTooltip:Show()
	else
		GameTooltip:SetText(tooltipText)
		GameTooltip:Show()
	end
end

--mapbutton
local function MapButton_OnUpdate(map)
	local subgroup,fileName,color
	local unit, icon, name, nameTemp
	if ( GetNumRaidMembers() > 0 ) then
		for i=1, MAX_RAID_MEMBERS do
			local partyMemberFrame = _G[map.."Raid"..i]
			unit = partyMemberFrame.unit
			if partyMemberFrame:IsShown() then
				icon = partyMemberFrame.icon
				for j=1, MAX_RAID_MEMBERS do
					nameTemp, _, subgroup, _, _, fileName = GetRaidRosterInfo(j)
					_, _, name = string.find(nameTemp or "", "([^%-]+)%-(.+)")
					if UnitName(unit) == name or UnitName(unit) == nameTemp then						
						icon:SetTexture(path .. "Group" .. subgroup)
						color = RAID_CLASS_COLORS[fileName]
						icon:SetVertexColor(color.r, color.g, color.b)
						break
					end
				end
			end
		end
	else
		for i=1, MAX_PARTY_MEMBERS do
			local partyMemberFrame = _G[map.."Party"..i]
			if partyMemberFrame:IsShown() then
				unit = partyMemberFrame.unit
				icon = partyMemberFrame.icon
				_, fileName = UnitClass(unit)
				icon:SetTexture(path .. "Normal")
				color = RAID_CLASS_COLORS[fileName]
				icon:SetVertexColor(color.r, color.g, color.b)
			end
		end
	end
end

--重写根据状态改变颜色的方法
function WorldMapUnit_Update(self)
	local unit = self.unit
	if not unit then return end

	local icon = self.icon
	local id = self:GetID()
	local name, nameTemp, fileName, fileNameTemp
	local color
	
	if unit == "party"..id then
		_, fileName = UnitClass(unit)
	elseif unit == "raid"..id then
		for j=1, MAX_RAID_MEMBERS do
			nameTemp, _, _, _, _, fileNameTemp = GetRaidRosterInfo(j)
			_, _, name = string.find(nameTemp or "", "([^%-]+)%-(.+)")
			if UnitName(unit) == name or UnitName(unit) == nameTemp then
				color = RAID_CLASS_COLORS[fileNameTemp]
				break
			end
		end
	else
		return
	end

	color = color or RAID_CLASS_COLORS[fileName]
	if UnitAffectingCombat(unit) then
		icon:SetVertexColor(0.8, 0, 0) 					-- flash red
	elseif UnitIsDeadOrGhost(unit) then
		icon:SetVertexColor(0.2, 0.2, 0.2) 				-- flash gray
	elseif PlayerIsPVPInactive(unit) then
		icon:SetVertexColor(0.5, 0.2, 0) 				-- flash some color
	elseif color then
		icon:SetVertexColor(color.r, color.g, color.b)
	else
		icon:SetVertexColor(0.8, 0.8, 0.8)
	end
end

--WorldMap
WorldMapButton:HookScript("OnUpdate", function()
	MapButton_OnUpdate("WorldMap")
end)

function WorldMapUnit_OnEnter(self, motion)
	MapUnit_OnEnter(self, motion, "WorldMap")
end

--BattlefieldMinimap
hooksecurefunc("BattlefieldMinimap_LoadUI", function()
	if BattlefieldMinimap then
		BattlefieldMinimap:HookScript("OnUpdate", function()
			MapButton_OnUpdate("BattlefieldMinimap")
		end)

		function BattlefieldMinimapUnit_OnEnter(self, motion)
			MapUnit_OnEnter(self, motion, "BattlefieldMinimap")
		end
	end
end)